<template>
  <BaseMainSlot>
    <template v-slot:dialogbar>
      <el-button class="selectAlls" type="text" @click="selectAll">全选</el-button>
      <el-button class="cancelSelectAlls" type="text" @click="cancelSelectAlls">取消全选</el-button>
      <ActionBar :has-add="false" :has-del="false" :has-edit="false" :has-cancel="false" :has-save="false" />
    </template>
    <template v-slot:content>
      <!-- <div class="tree"> -->
      <el-tree ref="tree" :props="treerule" :data="treeData" show-checkbox node-key="functionNo" accordion
        :default-checked-keys="defaultchecklist" @node-click="handleEdit" @check="handleSionChange"
        @check-change="handleSelectionChange" />
    </template>
  </BaseMainSlot>
</template>

<script>
import roleManage from "@/api/system-settings/role-manage";
import ActionBar from "@/components/ActionBar/index.vue";
import BaseMainSlot from "@/components/BaseMainSlot/dialog.vue";
import { mymixin } from "@/utils/mixins";
import axios from "axios";
export default {
  components: { BaseMainSlot, ActionBar },
  mixins: [mymixin],
  props: {
    roleNo: {
      type: String,
      default: ""
    },
    rolestu: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      treerule: {
        children: "childList",
        label: "functionName"
      },
      treeData: [],
      defaultchecklist: [],
      functionNos: [],
      userRouteMapperId: null,
      saveActive: false,
      cancelActive: false
    };
  },
  watch: {
    // id() {
    //   this.getfuncauth()
    // },
    rolestu: {
      handler(Val) {
        if (Val === "add") {
          this.save();
        }
      }
    }
  },
  created() {
    //   this.getdatamet()
  },
  methods: {
    // getdatamet() {
    //   let menuurlone;
    //   // let params = {}
    //   // let orgid = getToken("organizationNo")
    //   menuurlone = "v3/authority/v1/function/getSystemFunction";
    //   axios({
    //     url: this.$baseurl.webServiceUrl + menuurlone,
    //     // url: 'http://192.168.1.85:40029' + "/disease/add",
    //     // url: "http://192.168.1.85:8080/fileArg/receive",
    //     method: "get",
    //     headers: {
    //       databaseName: sessionStorage.databaseName,
    //       accessToken: sessionStorage.token,
    //       organizationNo: sessionStorage.organizationNo,
    //       organizationList: sessionStorage.organizationList,
    //       virtualSystemNo: "equipApp"
    //     }
    //   }).then(res => {
    //     if (res.data.success) {
    //       this.treeData = res.data.data || [];
    //       this.getfuncauth();
    //     } else {
    //       this.$message.warning("查询失败！");
    //     }
    //   });
    //   // axios.get(menuurlone).then(
    //   //   res => {
    //   //     this.treeData = res.data || []
    //   //     // console.log(res)
    //   //     // this.treeData.forEach(item => {
    //   //     //   item.metaName = item.meta.name
    //   //     //   this.$set(item, "metaName", item['meta']['title'])
    //   //     //   if (item.children) {
    //   //     //     item.children.forEach(itemTwo => {
    //   //     //     // itemTwo.metaName = itemTwo.meta.name
    //   //     //       this.$set(itemTwo, "metaName", itemTwo['meta']['title'])
    //   //     //     })
    //   //     //   }
    //   //     // })
    //   //     // console.log(this.treeData)
    //   //     this.getfuncauth()
    //   //   })
    //   // roleManage.gettreedata().then(res => {
    //   //   this.treeData = res.data
    //   //   // console.log(res)
    //   //   this.treeData.forEach(item => {
    //   //     item.metaName = item.meta.name
    //   //     this.$set(item, "metaName", item['meta']['title'])
    //   //     if (item.children) {
    //   //       item.children.forEach(itemTwo => {
    //   //         // itemTwo.metaName = itemTwo.meta.name
    //   //         this.$set(itemTwo, "metaName", itemTwo['meta']['title'])
    //   //       })
    //   //     }
    //   //   })
    //   //   // console.log(this.treeData)
    //   //   this.getfuncauth()
    //   // })
    //   // console.log("获取数据")
    // },
    // 获取所拥有的权限
    getfuncauth() {
      this.defaultchecklist = [];
      // console.log(this.userid)
      axios({
        url:
          this.$baseurl.webServiceUrl +
          `v3/authority/v1/function/getRoleFunction?roleNo=${this.roleNo}`,
        // url: 'http://192.168.1.85:40029' + "/disease/add",
        // url: "http://192.168.1.85:8080/fileArg/receive",
        method: "get",
        headers: {
          databaseName: sessionStorage.databaseName,
          accessToken: sessionStorage.token,
          organizationNo: sessionStorage.organizationNo,
          organizationList: sessionStorage.organizationList,
          virtualSystemNo: "equipApp"
        }
      }).then(res => {
        let PermissionList = res.data.data;
        // console.log(PermissionList)
        let that = this;
        that.$nextTick(() => {
          that.$refs.tree.setCheckedNodes([]);
        });
        PermissionList.forEach(item => {
          that.$nextTick(() => {
            // 设置某个节点
            that.$refs.tree.setChecked(item.functionNo, true);
          });
        });
        // this.checklist = nowcheck
        // if (res.data) {
        //   this.userRouteMapperId = '007'
        // }
      });
      // roleManage.getfuncauth(this.roleNo).then(res => {

      // })
    },
    setpermission() {
      let par = {
        id: this.userid,
        functionNos: this.functionNos
      };
      roleManage.addfuncauth(par).then(res => {
        this.getfuncauth();
      });
      // console.log("设置权限")
    },
    selectAll() {
      // this.flag = true;
      let that = this;
      that.$nextTick(() => {
        this.$refs.tree.setCheckedNodes(this.treeData);
      });
      this.handleSionChange();
    },
    cancelSelectAlls() {
      let that = this;
      that.$nextTick(() => {
        that.$refs.tree.setCheckedNodes([]);
      });
    },
    handleEdit(row) {
      // console.log(row)
    },
    handleSionChange() {
      this.saveActive = true;
      this.cancelActive = true;
    },
    handleSelectionChange() {
      let list = this.$refs.tree
        .getCheckedKeys()
        .concat(this.$refs.tree.getHalfCheckedKeys());
      // console.log(list)
      this.functionNos = list;
      // console.log(list)
      // if (this.handleSelectionChange) {
      // }
    },
    // 取消
    cancel() {
      this.canInput = false;
      this.addActive = false;
      this.delActive = false;
      this.saveActive = false;
      this.cancelActive = false;
      this.editActive = false;
      // let that = this
      // this.getfuncauth()
      // that.$nextTick(
      //   () => {
      //     that.$refs.tree.setCheckedNodes([])
      //   }
      // )
    },
    save() {
      // let par = {
      //   roleNo: this.roleNo,
      //   functionNos: this.functionNos
      // }
      axios({
        url: this.$baseurl.webServiceUrl + "v3/authority/v1/role/bindFunction",
        // url: 'http://192.168.1.85:40029' + "/disease/add",
        // url: "http://192.168.1.85:8080/fileArg/receive",
        method: "post",
        headers: {
          databaseName: sessionStorage.databaseName,
          accessToken: sessionStorage.token,
          organizationNo: sessionStorage.organizationNo,
          organizationList: sessionStorage.organizationList,
          virtualSystemNo: "equipApp"
        },
        data: `roleNo=${this.roleNo}&functionNos=${this.functionNos}`
      }).then(res => {
        if (res.data.success) {
          this.$message.success(res.message || "修改成功！");
          // this.getfuncauth()
          this.$emit("roles", "");
          this.cancel();
        } else {
          this.$message.error(res.message || "修改失败！");
        }
      });
      // roleManage.addfuncauth(`roleNo=${this.roleNo}&functionNos=${this.functionNos}`).then(res => {
      //   if (res.success) {
      //     this.$message.success(res.message || "修改成功！");
      //     this.getfuncauth()
      //     this.$emit('roles', '')
      //     this.cancel()
      //   } else {
      //     this.$message.error(res.message || "修改失败！");
      //   }
      // })
      // console.log("设置权限")
    }
  }
};
</script>
<style lang="scss" scoped>
@import "~@/styles/variables.scss";

.tree {
  width: 100%;
  height: 100%;
  padding-left: 10px;
  padding-right: 30px;
}

::v-deep .el-tree {
  height: 100%;
  width: 100%;

  .el-tree-node {
    margin: 0;
    // margin-bottom: 10px;
  }
}

::v-deep .common-layout {
  padding: 0 !important;

  // height: calc(100% - 5px) !important;
  .dialogcontent {
    height: 100% !important;
    margin: 0 !important;
  }
}

.selectAlls {
  position: absolute;
  height: 54px;
  width: 100px;
}

.cancelSelectAlls {
  position: absolute;
  height: 54px;
  width: 100px;
  margin-left: 100px;
}

.action-bar {
  background-color: #00485a;
}
</style>
